package com.op.studyingliteapp.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

import com.op.studyingliteapp.R;
import com.op.studyingliteapp.activity.homeactivity.HomeActivity;
import com.op.studyingliteapp.common.Common;
import com.op.studyingliteapp.task.LoginTask;
import com.op.studyingliteapp.task.LoginTask.LoginCallback;
import com.op.util.IntentUtils;
import com.op.util.SharedPreferenceUtil;
import com.op.util.StringUtils;
import com.op.util.ToastUtils;
import com.op.util.network.NetworkUtils;

/**
 * 显示欢迎界面，并进行相应的登录跳转
 * 
 * @author Michael
 * 
 */
public class SplashActivity extends Activity implements LoginCallback {

	private final long splashDelay = 2000L;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_splash);
		decideJump();
	}

	/**
	 * <ol>
	 * <li>无自动登录，跳转到LoginActivity</li>
	 * <li>有自动登录，无网络，跳转到HomeActivity</li>
	 * <li>有自动登录，有网络，发送登录请求</li>
	 * </ol>
	 */
	private void decideJump() {
		SharedPreferences preferences = SharedPreferenceUtil.getInstance(SplashActivity.this);
		boolean isAutoLogin = preferences.getBoolean(Common.Prefs.IS_AUTO_LOGIN, false);
		if (isAutoLogin) {
			String userNumber = preferences.getString(Common.Prefs.USER_NUMBER, "");
			String userPassword = preferences.getString(Common.Prefs.USER_PWD, "");
			if (NetworkUtils.isNetworkAvailable(this)) {
				requestLogin(userNumber, userPassword);
			} else {
				splash(splashDelay, HomeActivity.class); // 持续2秒后跳转
			}
		} else {
			splash(splashDelay, LoginActivity.class); // 持续2秒后跳转
		}
	}

	/**
	 * 发送用户名和密码，请求登录
	 * 
	 * @param userNumber
	 * @param userPassword
	 */
	private void requestLogin(String userNumber, String userPassword) {
		LoginTask task = LoginTask.newTask(SplashActivity.this, userNumber, userPassword);
		task.registerCallback(this);
		task.execute();
	}

	/**
	 * 处理登录响应
	 * 
	 * @param result
	 */
	@Override
	public void handleLoginResponse(String result) {
		try {
			// 获取数据为空，则直接跳转到HomeActivity
			if (StringUtils.isEmptyOrStringNull(result)) {
				splash(splashDelay, HomeActivity.class);
				return;
			}

			JSONObject jsonObject = new JSONObject(result);
			String loginMsg = jsonObject.getString(Common.JsonResult.MESSAGE);

			if (loginMsg.equals(Common.JsonResult.LOGIN_SUCCESS)) {
				ToastUtils.show(SplashActivity.this, R.string.tips_login_success);
				LoginTask.saveData(SplashActivity.this, jsonObject);
				splash(splashDelay, HomeActivity.class);
			} else if (loginMsg.equals(Common.JsonResult.LOGIN_FAIL)) {
				ToastUtils.show(SplashActivity.this, R.string.tips_login_fail);
				splash(splashDelay, LoginActivity.class);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 持续指定时间后跳转
	 */
	private void splash(long delayMillis, final Class<?> cls) {

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				IntentUtils.jump(SplashActivity.this, cls);
				finish();
			}
		}, delayMillis);
	}

}
